package com.kgc.controller;

import com.kgc.bean.User;
import com.kgc.config.annotation.RequestPermission;
import com.kgc.config.annotation.RequestUser;
import com.kgc.service.RegisterAndLoginService;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Classname RegisterAndLoginController
 * @Description TODO
 * @Date 2021/6/21 10:07
 * @Created by 17402
 */
@RestController
@Api(tags = "登录注册入口")
@Slf4j
public class RegisterAndLoginController {
    @Autowired
    private RegisterAndLoginService registerAndLoginService;
    //发送验证码
    @PostMapping("/sendCode")
    @ApiOperation(value = "发送验证码",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "发送验证码成功"),
            @ApiResponse(code = 1001,message = "发送验证码失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "传入手机号", required = true),
    })
    ReturnResult<Map<String,Object>> sendCode(String phone){
        return registerAndLoginService.sendCode(phone);
    }


    //手机号加验证码登录或注册
    @PostMapping("/userCode2logInOrRegistered")
    @ApiOperation(value = "手机号加验证码登录或注册",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功"),
            @ApiResponse(code = 1002,message = "token错误，重新登录"),
            @ApiResponse(code = 1003,message = "验证码错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "传入手机号", required = true),
            @ApiImplicitParam(name = "code", value = "传入验证码", required = true)
    })
    ReturnResult<Map<String,Object>> userCode2logInOrRegistered(@RequestParam String phone, @RequestParam String code, HttpServletRequest request, HttpServletResponse resp){
        return registerAndLoginService.userCode2logInOrRegistered(phone,code,request,resp);
    }
    //获取微信授权登录二维码
    @PostMapping("/getWxLoginQRCode")
    @ApiOperation(value = "获取微信授权登录二维码",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功")
    })
    ReturnResult<Map<String,Object>> getWxLoginQRCode(){
        return registerAndLoginService.getWxLoginQRCode();
    }

    //微信授权回调
    @ApiIgnore
    @RequestMapping(value = "/Callback", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    ReturnResult<Map<String,Object>> wechatAuthorizationCallback(HttpServletRequest request, HttpServletResponse resp){

        return registerAndLoginService.wechatAuthorizationCallback(request,resp);
    }
    //修改个人信息
    @PostMapping("/editPersonalInformation")
    @RequestPermission
    @ApiOperation(value = "修改个人信息",notes = "")
    @ApiResponses({
            @ApiResponse(code = 1002,message = "token错误，重新登录"),
            @ApiResponse(code = 1005,message = "用户名已存在"),
            @ApiResponse(code = 1006,message = "手机号已存在"),
            @ApiResponse(code = 0000,message = "成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "修改账户名", required = false),
            @ApiImplicitParam(name = "pwd", value = "修改密码", required = false),
            @ApiImplicitParam(name = "nickname", value = "修改昵称", required = false),
            @ApiImplicitParam(name = "sex", value = "修改性别", required = false),
            @ApiImplicitParam(name = "phone", value = "修改手机号", required = false),
            @ApiImplicitParam(name = "country", value = "修改国家", required = false),
            @ApiImplicitParam(name = "province", value = "修改省份", required = false)

    })
    ReturnResult<Map<String,Object>> editPersonalInformation(String username, String pwd, String nickname, String sex, String phone, String country, String province,
                                                             @ApiIgnore @RequestUser User user, HttpServletRequest request){
        if (null==user){
            ResultBuildUtil.fail("1002","token错误，重新登录");
        }
        log.info("**************user:{}**********************",user.toString());
        log.info("**************token:{}**********************",request.getHeader("token"));
        return registerAndLoginService.editPersonalInformation(username,pwd,nickname,sex,phone,country,province,user,request.getHeader("token"));
    }
    //查看个人信息
    @RequestPermission
    @PostMapping("/viewPersonalInformation")
    @ApiOperation(value = "查看个人信息",notes = "")
    @ApiResponses({
            @ApiResponse(code = 1002,message = "token错误，重新登录"),
            @ApiResponse(code = 0000,message = "成功")
    })
    ReturnResult<User> viewPersonalInformation(@ApiIgnore @RequestUser User user){
        if(user==null||user.getUserId()==null){
            log.info("-----------------user；{}",user);
            return ResultBuildUtil.fail("1002","token错误，重新登录");
        }
        return ResultBuildUtil.success(user);
    }
}
